package com.sharefang.aproject.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.sharefang.aproject.adapter.XRecyclerViewAdapter;
import com.sharefang.aproject.bean.NppBean;
import com.sharefang.aproject.constant.FragmentType;
import com.sharefang.aproject.net.NetApi;
import com.sharefang.aproject.presenter.GetNppPresenter;
import com.sharefang.aproject.utils.UiUtil;
import com.sharefang.aproject.view.INppView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 推荐页
 * Created by Administrator on 2016/8/16.
 */

@SuppressLint("ValidFragment")
public class HotFragment extends RecyclerViewFragment implements INppView, NetApi {
    GetNppPresenter nppPresenter = new GetNppPresenter(NET_HOT, this);
    Context context;
    private int offset = -1;
    Map<String, Object> map;

    public HotFragment(Context context) {
        this.context = context;
        map = new HashMap<>();
        map.put("page", 1);
        map.put("offset", offset);
        map.put("limit", 10);

        nppPresenter.getNpp(map, false);
    }

    @Override
    void doRefresh() {
        offset = -1;
        map.remove("offset");
        map.put("offset", offset);
        nppPresenter.getNpp(map, false);
    }

    @Override
    void doLoadMore() {
        offset += 11;
        map.remove("offset");
        map.put("offset", offset);
        nppPresenter.getNpp(map, true);
    }

    @Override
    void setLayoutManager() {
        layoutManager = new GridLayoutManager(context, 2);
    }

    @Override
    void setAdapter() {
        adapter = new XRecyclerViewAdapter(context, FragmentType.HOT);
    }

    @Override
    void onItemClick(View view, int position) {

    }

    @Override
    void onItemLongClick(View view, int position) {

    }

    @Override
    public void doGetNpp(final List<NppBean> nppBeens, boolean loadMore) {
        if (loadMore) {
            if (nppBeens.size() != 0) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        adapter.setNppData(nppBeens);
                        adapter.notifyDataSetChanged();
                        xRecyclerView.loadMoreComplete();
                    }
                }, 500);
            } else {
                xRecyclerView.setIsnomore(true);
            }
        } else {
            //刷新
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    adapter.resetNppData(nppBeens);
                    adapter.notifyDataSetChanged();
                    xRecyclerView.refreshComplete();
                    xRecyclerView.reset();
                }
            }, 500);
        }
    }

    @Override
    public void showFailedError(String message) {
        UiUtil.snake((Activity) context, message);
        xRecyclerView.reset();
    }
}
